package cn.touna.jss.modules.third.tencent.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Random;

/**
 ********************************************************
 * TencentOcrUtil工具类
 * @author zhangjialian
 * @Date 2018-08-10 15:39
 * @Version V1.0
 ********************************************************/
public class TencentOcrUtil {
    /**
     ********************************************************************
     * 生成 Authorization 签名字段
     * @param appId 应用主账号ID
     * @param secretId 加密键ID
     * @param secretKey 加密值
     * @param bucketName bucket名称
     * @param expired 超时时间,单位秒
     * @Author  zhangjialian
     * @return  java.lang.String
     * @date    2018/8/10 15:35
     ********************************************************************/
    public static String appSign(long appId, String secretId, String secretKey, String bucketName,
                                 long expired) throws Exception {
        long now = System.currentTimeMillis() / 1000;
        int rdm = Math.abs(new Random().nextInt());
        String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d", appId, bucketName,
                secretId, now, now + expired, rdm);
        byte[] hmacDigest = HmacSha1(plainText, secretKey);
        byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
        System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
        System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length,
                plainText.getBytes().length);
        return Base64Encode(signContent);
    }

    /**
     ********************************************************************
     * 生成base64编码
     * @param binaryData 二进制签名数据
     * @Author  zhangjialian
     * @return  java.lang.String
     * @date    2018/8/10 15:37
     ********************************************************************/
    public static String Base64Encode(byte[] binaryData) {
        String encodedstr = Base64.getEncoder().encodeToString(binaryData);
        return encodedstr;
    }

    /**
     ********************************************************************
     * 组装的Byte数据生成hmacsha签名
     * @param binaryData 组装的字符byte数据
     * @param key 密钥
     * @Author  zhangjialian
     * @return  byte[]
     * @date    2018/8/10 15:38
     ********************************************************************/
    public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(secretKey);
        byte[] HmacSha1Digest = mac.doFinal(binaryData);
        return HmacSha1Digest;
    }

    /**
     ********************************************************************
     * 组装的字符数据生成hmacsha签名
     * @param plainText 组装的字符数据
     * @param key 密钥
     * @Author  zhangjialian
     * @return  byte[]
     * @date    2018/8/10 15:38
     ********************************************************************/
    public static byte[] HmacSha1(String plainText, String key) throws Exception {
        return HmacSha1(plainText.getBytes(), key);
    }
}
